<template>
  <div id="reg">
    <el-row type="flex" justify="center">
      <el-col :span="8">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm"
                 label-position="left" label-width="0px" align="center">
          <h3 class="reg_title">系统注册</h3>
          <el-form-item prop="username" class="in">
            <el-input v-model="ruleForm.username" placeholder="用户名"></el-input>
          </el-form-item>
          <el-form-item prop="pass" class="in">
            <el-input v-model="ruleForm.pass" autocomplete="off" placeholder="密码" show-password></el-input>
          </el-form-item>
          <el-form-item prop="checkPass" class="in">
            <el-input v-model="ruleForm.checkPass" autocomplete="off" placeholder="确认密码" show-password></el-input>
          </el-form-item>
          <el-form-item prop="name" class="in">
            <el-input v-model="ruleForm.name" placeholder="姓名"></el-input>
          </el-form-item>
          <el-form-item prop="idNumber" class="in">
            <el-input v-model="ruleForm.idNumber" placeholder="身份证号"></el-input>
          </el-form-item>
          <el-form-item prop="age" class="in">
            <el-input v-model="ruleForm.age" placeholder="年龄"></el-input>
          </el-form-item>
          <el-form-item prop="phone" class="in">
            <el-input v-model="ruleForm.phone" placeholder="手机号"></el-input>
          </el-form-item>
          <el-form-item prop="email" class="in">
            <el-input v-model="ruleForm.email" @keyup.native.enter="submitForm('ruleForm')" placeholder="邮箱"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 40%;background: #505458;border: none" v-on:click="submitForm('ruleForm')">注册</el-button>
            <!--<el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>-->
            <!--<el-button @click="resetForm('ruleForm')">重置</el-button>-->
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

  </div>
</template>


<script>
  export default {
    data() {
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };

      return {
        ruleForm: {
          username: '',
          pass: '',
          name: '',
          idNumber:'',
          age:'',
          phone: '',
          email:'',
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
          ],
          pass: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入昵称', trigger: 'blur' },
          ],
          idNumber: [
            { required: true, message: '请输入身份证号', trigger: 'blur' },
          ],
          age: [
            { required: true, message: '请输入年龄', trigger: 'blur' },
          ],
          phone: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
          ],
          email: [
            { required: true, message: '请输入邮箱', trigger: 'blur' },
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // http://176.17.10.223:8080/admins/add-new?username=root&password=root
            let url = 'http://176.17.10.223:18080/user/add-new'
            let data = 'username='+ this.ruleForm.username +
                    "&password=" + this.ruleForm.pass +
                    "&name=" + this.ruleForm.name +
                    "&idNumber=" + this.ruleForm.idNumber +
                    "&age=" + this.ruleForm.age +
                    "&phone=" + this.ruleForm.phone +
                    "&email=" + this.ruleForm.email;
            console.log("请求路径: " + url);
            console.log("请求数据: " + data);
            // 用data这种json格式的数据的话需要后端允许加@RequestBody
            /*let data = {
              "password": this.ruleForm.pass,
              "username": this.ruleForm.username,
              "nickname": this.ruleForm.nickname,
              "phone": this.ruleForm.phone,
              "email": this.ruleForm.email,
            }*/
            this.axios.post(url, data).then((response) => {
              let message = response.data.message;
              if (message == null){
                this.$message({
                  message: '恭喜你，注册成功',
                  type: 'success',
                });
                this.$router.push({path: '/login'});  //页面路由刷新
              }else {
                console.log(message);
                this.$message.error(message);
              }
            })
                    .catch(function (error) {
                      console.log(error);
                    });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style>
  body{
    margin: 0;
  }
  .in input.el-input__inner{
    border-radius: 20px;
  }
  .demo-ruleForm{
    /*position: center;*/
    /*overflow: auto;*/
    margin-top: 170px;

    padding: 15px;
    background-color: #8c939d;
    background-color:rgba(0,0,20,0.4);
  }
  .reg_title{
    color: pink;
  }

  #reg {
    margin: 0 auto;
    background: url(../img/eva2.jpg) no-repeat;
    background-position: center;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background-size: 100% 100%;
    position: fixed;
    overflow: auto;
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #2c3e50;
  }

  nav {
    padding: 30px;
  }

  nav a {
    font-weight: bold;
    color: #2c3e50;
  }

  nav a.router-link-exact-active {
    color: #42b983;
  }
</style>

